package com.example.myhelloworld.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class TestExercise extends ExerciseClass{
	DockPanel vert = new DockPanel();
	int activeExercise = 0;  		//the current active exercise, keeps track on what exercise to print.
	String[] exercises;
	int numberOfExercises;
	Panel activeExercisePanel;
	produceExercise exCreater;
	double result = 0;
	
	public TestExercise(String uName, String settings,
			GreetingServiceAsync GSA) {
		super(uName, settings, GSA);
		vert.setSpacing(10);
		
		this.selfPanel = vert;
		
		produceResultButton = new Button("Test Done!");
		Label title = new Label("Testing the test!");
		vert.add(title, DockPanel.NORTH);
		vert.addStyleName("ExerciseBasic");
		vert.addStyleName("TestExercise");
		
		exercises = settings.split(",");
		numberOfExercises = exercises.length;
		
		exCreater = new produceExercise(uName,GSA);
		
		printNextExerciseInTest();

//		"plusMinusDivTimes:Plus, nasta osv."
//		syntax for input: klassnamn + ':' + installnig, lage + "," + nasta klassnamn
		
	}
	public void removeExercise(DockPanel dpan){
		int count = dpan.getWidgetCount();
		for (int i=0;i<count;i++){
			Widget trial = dpan.getWidget(i);
//			System.out.println("Any difference? testString = \"" + teststring + "\" , trial.getStyleName() = \"" + trial.getStyleName()+ "\" Compare: (teststring == trial.getStyleName()) = " + (teststring == trial.getStyleName()));
			if (trial.getStyleName().contains("ExerciseBasic") || trial.getStyleName().contains("sudoku") || trial.getStyleName().contains("Exercise")){
				trial.removeFromParent();
			}
		}
	}
	
	public void printNextExerciseInTest(){
		String currentClass 	= exercises[activeExercise].split(":")[0];
		String currentParameter = exercises[activeExercise].split(":")[1];
		
		ExerciseClass exerciseActive = exCreater.exerciseFromString(exercises[activeExercise]);

		vert.add(exerciseActive.getSelfPanel(), DockPanel.CENTER);
		exerciseActive.getProduceResultButton().addClickHandler(new drawNew(this,exerciseActive));

		activeExercise++;
	}

	@Override
	public void draw()  {	
	}

	@Override
	public String pushresult() {
		return Integer.toString((int)result);
	}

	private class drawNew implements ClickHandler{
		TestExercise testEx;
		ExerciseClass exPan;
		private drawNew(TestExercise test, ExerciseClass ex){
			testEx = test;
			exPan = ex;
		}
		
		public void onClick(ClickEvent event) {
			// TODO Auto-generated method stub
			
			if (testEx.activeExercise == testEx.numberOfExercises){
				exPan.getSelfPanel().removeFromParent();
				System.out.println("All tests done ");
//				produceResultButton = new Button("Test Done!");
				testEx.vert.add(produceResultButton, DockPanel.SOUTH);
				//total points / number of exercises = average points.
				result = (result / testEx.numberOfExercises);
			} else {
				result = result + Integer.parseInt(exPan.pushresult());
				exPan.getSelfPanel().removeFromParent();
				testEx.printNextExerciseInTest();	
			}
		}
	}
}


